// 调试过滤功能的脚本
// 在浏览器控制台中运行此脚本来测试过滤功能

console.log('=== 开始测试过滤功能 ===')

// 1. 检查响应式变量状态
function checkReactiveVariables() {
  console.log('--- 检查响应式变量状态 ---')
  
  // 检查搜索文本
  console.log('searchText:', window.searchText?.value)
  console.log('debouncedSearchText:', window.debouncedSearchText?.value)
  
  // 检查时间范围
  console.log('startTime:', window.startTime?.value)
  console.log('endTime:', window.endTime?.value)
  
  // 检查级别过滤
  console.log('levelFilters:', window.levelFilters?.value)
  
  // 检查组件搜索文本
  console.log('componentSearchTexts:', window.componentSearchTexts?.value)
  console.log('debouncedComponentSearchTexts:', window.debouncedComponentSearchTexts?.value)
  
  // 检查缓存键
  console.log('cacheKey:', window.cacheKey?.value)
}

// 2. 测试搜索过滤
function testSearchFilter() {
  console.log('--- 测试搜索过滤 ---')
  
  // 设置搜索文本
  if (window.searchText) {
    window.searchText.value = 'test'
    console.log('设置搜索文本为 "test"')
    
    // 等待防抖
    setTimeout(() => {
      console.log('防抖后搜索文本:', window.debouncedSearchText?.value)
      console.log('缓存键更新:', window.cacheKey?.value)
    }, 350)
  }
}

// 3. 测试级别过滤
function testLevelFilter() {
  console.log('--- 测试级别过滤 ---')
  
  // 检查当前选中的组件
  const selectedComponents = window.selectedComponents?.value
  console.log('选中的组件:', selectedComponents)
  
  if (selectedComponents && selectedComponents.length > 0) {
    const component = selectedComponents[0]
    console.log(`测试组件: ${component}`)
    
    // 检查该组件的级别过滤状态
    const levelFilter = window.levelFilters?.value.get(component)
    console.log(`组件 ${component} 的级别过滤:`, levelFilter)
    
    // 测试切换级别过滤
    if (window.toggleLevelFilter) {
      console.log(`尝试禁用组件 ${component} 的 ERROR 级别`)
      window.toggleLevelFilter(component, 'ERROR', false)
      
      setTimeout(() => {
        console.log(`组件 ${component} 的级别过滤更新后:`, window.levelFilters?.value.get(component))
      }, 100)
    }
  }
}

// 4. 测试时间过滤
function testTimeFilter() {
  console.log('--- 测试时间过滤 ---')
  
  // 设置时间范围
  if (window.startTime && window.endTime) {
    const now = new Date()
    const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000)
    const oneHourLater = new Date(now.getTime() + 60 * 60 * 1000)
    
    window.startTime.value = oneHourAgo.toISOString()
    window.endTime.value = oneHourLater.toISOString()
    
    console.log('设置时间范围:', {
      start: window.startTime.value,
      end: window.endTime.value
    })
    
    // 等待缓存键更新
    setTimeout(() => {
      console.log('时间过滤后缓存键:', window.cacheKey?.value)
    }, 100)
  }
}

// 5. 检查缓存状态
function checkCacheStatus() {
  console.log('--- 检查缓存状态 ---')
  
  if (window.filteredLogsCache) {
    console.log('过滤日志缓存大小:', window.filteredLogsCache.value.size)
    console.log('缓存键列表:', Array.from(window.filteredLogsCache.value.keys()))
  }
}

// 6. 测试组件日志获取
function testComponentLogs() {
  console.log('--- 测试组件日志获取 ---')
  
  const selectedComponents = window.selectedComponents?.value
  if (selectedComponents && selectedComponents.length > 0) {
    const component = selectedComponents[0]
    console.log(`测试获取组件 ${component} 的日志`)
    
    // 测试同步获取
    if (window.getComponentLogsSync) {
      const logs = window.getComponentLogsSync(component)
      console.log(`组件 ${component} 同步日志数量:`, logs?.length)
    }
    
    // 测试异步获取
    if (window.getFilteredComponentLogs) {
      window.getFilteredComponentLogs(component).then(logs => {
        console.log(`组件 ${component} 过滤后日志数量:`, logs?.length)
      })
    }
  }
}

// 7. 运行所有测试
function runAllTests() {
  console.log('开始运行所有测试...')
  
  checkReactiveVariables()
  
  setTimeout(() => {
    testSearchFilter()
  }, 100)
  
  setTimeout(() => {
    testLevelFilter()
  }, 500)
  
  setTimeout(() => {
    testTimeFilter()
  }, 1000)
  
  setTimeout(() => {
    checkCacheStatus()
    testComponentLogs()
  }, 1500)
}

// 8. 监听过滤变化
function watchFilterChanges() {
  console.log('--- 开始监听过滤变化 ---')
  
  // 监听搜索文本变化
  if (window.searchText) {
    const originalValue = window.searchText.value
    const unwatch = window.watch(window.searchText, (newVal, oldVal) => {
      console.log('搜索文本变化:', { old: oldVal, new: newVal })
    })
    console.log('已监听搜索文本变化')
  }
  
  // 监听级别过滤变化
  if (window.levelFilters) {
    const unwatch = window.watch(window.levelFilters, (newVal, oldVal) => {
      console.log('级别过滤变化:', { old: oldVal, new: newVal })
    }, { deep: true })
    console.log('已监听级别过滤变化')
  }
  
  // 监听缓存键变化
  if (window.cacheKey) {
    const unwatch = window.watch(window.cacheKey, (newVal, oldVal) => {
      console.log('缓存键变化:', { old: oldVal, new: newVal })
    })
    console.log('已监听缓存键变化')
  }
}

// 导出测试函数
window.testFiltersDebug = {
  checkReactiveVariables,
  testSearchFilter,
  testLevelFilter,
  testTimeFilter,
  checkCacheStatus,
  testComponentLogs,
  runAllTests,
  watchFilterChanges
}

console.log('过滤功能调试脚本已加载')
console.log('使用方法: testFiltersDebug.runAllTests()')
console.log('监听变化: testFiltersDebug.watchFilterChanges()')
